/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.panels;

import data.User;

import data.logic.WorldLogic;
import gui.AbstrWorld;
import gui.MaiaWorld;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import maiaclient.Maia;
import maiaclient.interfaces.IFriendListener;

/**
 *
 * @author groep 11
 * @date 2007-2008
 */
public class FriendsControl extends JPanel implements IFriendListener {

    private WorldMap pnlWorldMap;
    private WorldLogic worldInfo;
    private JLabel lblTitle = null;
    private JPanel pnlTop = null;
    private JPanel pnlMiddle = null;
    private JTextField txtName = null;
    private JButton btnAdd = null;
    private JButton btnRemove = null;
    private JList lstFriends = null;
    private DefaultListModel lstFriendsData = null;
    private JPanel pnlButtons = null;
    private AbstrWorld parent = null;
    private Maia maia;

    public FriendsControl(Maia maia, AbstrWorld parentFrame) {
        this.parent = parentFrame;
        this.maia = maia;
        maia.addFriendListener(this);
        initPanel();

    }

    Object[] getUsers() {
        return lstFriendsData.toArray();
    }

    public WorldMap getPnlWorldMap() {
        return pnlWorldMap;
    }

    public void setPnlWorldMap(WorldMap pnlWorldMap) {
        this.pnlWorldMap = pnlWorldMap;
    }

    public WorldLogic getWorldInfo() {
        return worldInfo;
    }

    public void setWorldInfo(WorldLogic worldInfo) {
        this.worldInfo = worldInfo;
    }

    private void initPanel() {


        setPreferredSize(new Dimension(200, 0));

        setLayout(new BorderLayout());

        lblTitle = new JLabel("Control Friends");
        pnlTop = new JPanel(new BorderLayout(5, 5));
        pnlTop.setBorder(new EmptyBorder(10, 5, 0, 0));

        pnlTop.add(lblTitle, BorderLayout.NORTH);
        add(pnlTop, BorderLayout.NORTH);

        //---------------------------Midden------------------------------------
        //---midden panel bestaat uit een invoerveld, 2 knoppen en een lijst---
        pnlMiddle = new JPanel(new BorderLayout(5, 5));

        txtName = new JTextField("find with username");
        txtName.setEnabled(false);

        btnAdd = new JButton("Add");
        btnAdd.setEnabled(false);
        btnAdd.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
            });

        btnRemove = new JButton("Remove");
        btnRemove.setEnabled(false);
        btnRemove.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
            });

        lstFriendsData = new DefaultListModel();
        lstFriends = new JList(lstFriendsData);
        lstFriends.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        //panel voor de knoppen
        pnlButtons = new JPanel(new GridLayout(1, 2));

        pnlButtons.add(btnAdd);
        pnlButtons.add(btnRemove);
        pnlMiddle.add(txtName, BorderLayout.NORTH);
        pnlMiddle.add(lstFriends, BorderLayout.CENTER);
        pnlMiddle.add(pnlButtons, BorderLayout.SOUTH);
        add(pnlMiddle, BorderLayout.CENTER);

        if (parent instanceof MaiaWorld) {
            // ask server for friendlist
            try {
                parent.getMaia().requestFriends();
            } catch (IOException ex) {
                Logger.getLogger(FriendsControl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void setControlsDisabled() {
        txtName.setEnabled(false);
        btnAdd.setEnabled(false);
        btnRemove.setEnabled(false);
        lstFriends.setEnabled(false);
        lblTitle.setEnabled(false);

    }

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
        try {

            User usr = maia.getUserByName(this.txtName.getText());

            if (usr == null) {
                //gebruiker niet gevonden
                JOptionPane.showMessageDialog(this, "User with username: " + txtName.getText() + " doesn't exist", "Add Friends", JOptionPane.WARNING_MESSAGE);
            } else {

                //toevoegen aan de vriendelijst;
                lstFriendsData.addElement(usr);

                pnlWorldMap.repaint();
            }
        } catch (IOException ex) {
            Logger.getLogger(FriendsControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FriendsControl.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {
        if (lstFriends.getSelectedValue() == null) {
            JOptionPane.showMessageDialog(this, "Please select a user", "Remove friend", JOptionPane.WARNING_MESSAGE);
        } else {
            User gebr = (User) lstFriends.getSelectedValue();
            lstFriendsData.removeElement(lstFriends.getSelectedValue());
//            worldInfo.removeFriend(gebr.getUserId());
            pnlWorldMap.repaint();
        }


    }

    public void updateFriends(Vector<User> friends) {

        lstFriendsData.removeAllElements();
        for (User friend : friends) {
            lstFriendsData.addElement(friend);
        }

    }
}
